using Microsoft.AspNetCore.Mvc;
using ShopForWeb.Dto.AjaxModels;
using ShopForWeb.Dto.ViewModels;
using ShopForWeb.Service;
using ShopForWeb.UI.Models;
using ShopForWeb.UI.Extensions;
using ShopForWeb.UI.Filters;

namespace ShopForWeb.UI.Controllers.Admin;

[Area("admin")]
[Route("[area]/commoditys")]
[ManagerAuthorizeFilter]
public class CommodityController : Controller
{
  private CommodityService _service;
  private IWebHostEnvironment _host;
  public CommodityController(CommodityService service, IWebHostEnvironment host)
  {
    _service = service;
    _host = host;
  }
  //[HttpGet("commoditys/{page?}/{pageSize?}/{key?}")]
  [ManagerInfoFilter]
  [HttpGet("{page?}/{pageSize?}/{key?}")]
  public async Task<IActionResult> GetPageAsync([ModelBinder(Name = "page")] int page = 1, [ModelBinder(Name = "pageSize")] int pageSize = 15, [ModelBinder(Name = "key")] string key = "")
  {
    // throw new Exception("异常测试");
    var commoditys = await _service.GetByPageAsync((page - 1) * pageSize, pageSize, key);
    var total = await _service.GetCountAsync(key);
    PageBarViewModel pagebar = new PageBarViewModel(page, pageSize, total);
    ViewBag.Data = new
    {
      Commoditys = commoditys,
      PageBar = pagebar
    };

    return View();
    // return Json(new ResponseAjaxModel()
    // {
    //   Status = 0,
    //   Data = data,
    //   Total = total
    // });
  }
  [HttpPut("info")]
  public async Task<IActionResult> AddNewAsync(CommodityEditViewModel pvm)
  {
    if (pvm.PhotoFile != null)
      pvm.Photo = await this.FileSaveAsync(pvm.PhotoFile, Path.Combine(_host.WebRootPath, "upload"), pvm.Photo);
    int result = await _service.AddNewAsync(pvm);
    if (result > 0)
      return Json(new ResponseAjaxModel()
      {
        Status = 0,
        Message = "添加成功",
      });
    else
      return Json(new ResponseAjaxModel()
      {
        Status = 1,
        Message = "添加失败",
      });
  }
  [ManagerInfoFilter]
  [HttpGet("info/{commodityID}")]
  public async Task<IActionResult> GetByIDAsync(int commodityID)
  {
    var p = await _service.GetByIDAsync(commodityID);
    ViewBag.Data = p;
    return View();
    // if (p != null)
    //   return Json(new ResponseAjaxModel
    //   {
    //     Status = 0,
    //     Data = p
    //   });
    // else
    //   return Json(new ResponseAjaxModel
    //   {
    //     Status = 1,
    //     Data = "没有此商品"
    //   });
  }
  [HttpPost("info")]
  public async Task<IActionResult> ModifyAsync(CommodityEditViewModel pvm)
  {
    if (pvm.PhotoFile != null)
      pvm.Photo = await this.FileSaveAsync(pvm.PhotoFile, Path.Combine(_host.WebRootPath, "upload"), pvm.Photo);
    int result = await _service.ModifyAsync(pvm);
    if (result > 0)
      return Json(new ResponseAjaxModel
      {
        Status = 0,
        Message = "修改成功"
      });
    else
      return Json(new ResponseAjaxModel
      {
        Status = 1,
        Message = "修改成功"
      });
  }

  [HttpDelete("info/{commodityID}")]
  public async Task<IActionResult> RemoveAsync(int commodityID)
  {
    int result = await _service.RemoveAsync(commodityID);
    if (result > 0)
      return Json(new ResponseAjaxModel
      {
        Status = 0,
        Message = "删除成功"
      });
    else
      return Json(new ResponseAjaxModel
      {
        Status = 1,
        Message = "删除成功"
      });
  }

}
